package com.filter;

import javax.servlet.http.*;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.bean.UserInfo;

/**
 * @author hhh
 *登录验证器,如果不在登录状态则拦截请求
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {
    private static final String[] IGNORE_URI ={"/login"};
 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String url = request.getRequestURL().toString();
        response.setCharacterEncoding("utf-8");
        for (String s : IGNORE_URI) {
            if (url.contains(s)) {
                return true;
            }
        }
        HttpSession session=request.getSession();
        	UserInfo name=(UserInfo)session.getAttribute("pass");
        	if(name!=null){
        		Long deadline=(Long)session.getAttribute(url+session.getId());
        		if(deadline==null||deadline<System.currentTimeMillis()){
        			
        		session.setAttribute(url+session.getId(), System.currentTimeMillis()+5000);
        		return true;
        		}else{
        			response.getWriter().write("您的提交太频繁了");
        			return false;
        		}
        	}
        	else{
        		
        		response.getWriter().write("请登录");
        		return false;
        	}
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }
}